package com.two.lbms.xhz.controller;
import com.two.lbms.base.BaseController;
import com.two.lbms.base.ResultInfo;
import com.two.lbms.vo.CollectionList;
import com.two.lbms.xhz.dto.CollectionDTO;
import com.two.lbms.xhz.service.BookCaseService;
import com.two.lbms.xhz.model.CollectionQuery;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/bookcase")
@Slf4j
public class BookCaseController extends BaseController {
    @Resource
    private BookCaseService bookCaseService;

    /**
     * 分页查询
     */
    @GetMapping("list")
    @ResponseBody
    public Map<String, Object> selectByParams(CollectionQuery collectionQuery) {
        return bookCaseService.queryByParamsForTable(collectionQuery);
    }
    /**
     * 进入馆藏管理页面
     * */
    @RequestMapping("index")
    public String toBookCasePage(){
        return "xhz/bookcase";
 }

    /**
     * 添加书本
     */
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addBookInfo(CollectionQuery collectionQuery) {
        log.info("添加馆藏数据 : {}",collectionQuery);
        bookCaseService.addBookInfo(collectionQuery);
        return success("书本信息添加成功！");
    }
    /**
     * 更新书本
     * */
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateBookInfo(CollectionQuery collectionQuery){
        log.info("要修改的馆藏信息 : {}",collectionQuery);
        bookCaseService.updateBookInfo(collectionQuery);
        return success("书本信息更新成功！");
    }
    /**
     * 打开添加/修改用户的对话框
     * */
    @RequestMapping("toAddOrUpdateBookPage")
    public String toAddOrUpdateBookPage(Integer id, HttpServletRequest request){
        if(id != null) {
            //通过ID查询用户对象
            CollectionDTO collectionDTO = bookCaseService.selectById(id);
            log.info("填充前端信息的馆藏 : {}",collectionDTO);
            //将数据设置到请求域中
            request.setAttribute("collection", collectionDTO);
        }
        return "/xhz/add.update";
    }
    /**
     * 删除书本
     * */
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteBookInfo(Integer[] bookIds){
        bookCaseService.deleteBookInfo(bookIds);
        return success("书本信息删除成功！");
    }
    @DeleteMapping("deleteBook")
    @ResponseBody
    public ResultInfo deleteBookInfo(Integer id){
        bookCaseService.deleteBook(id);
        return success("删除馆藏信息成功！");
    }
}